<template>
  <div class="login_container">
    <div class="login_form">
      <div class="headImage">
        <img src="../assets/image/360downloadswallcopy.jpg" alt="" />
      </div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="ruleForm.name"
            autocomplete="off"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
            prefix-icon="el-icon-key"
          ></el-input>
        </el-form-item>

        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
            prefix-icon="el-icon-check"
          ></el-input>
        </el-form-item>

        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 注册提示部分 -->
      <div class="info-section">
        <p>还没有账户？</p>
        <el-button type="text" @click="navigateToSignup">立即注册</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "UserLogin",
  data() {
    var validatePass = (rule, value, callback) => {
      if (this.ruleForm.checkPass !== "") {
        this.$refs.ruleForm.validateField("checkPass");
      }
      callback();
    };
    var validatePass2 = (rule, value, callback) => {
      if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        password: "",
        checkPass: "",
        name: "",
      },
      rules: {
        name: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [
          { validator: validatePass, trigger: "blur" },
          { required: true, message: "请输入密码" },
          { min: 6, message: "密码位数至少为6" },
        ],
        checkPass: [
          { validator: validatePass2, trigger: "blur" },
          { required: true, message: "请再次输入密码" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const result = await this.$http.post("webPage/user/login", {
            name: this.ruleForm.name,
            password: this.ruleForm.password,
          });
          if (result.data.code === 1) {
            this.$message({
              message: "恭喜你，登陆成功",
              type: "success",
            });
            window.sessionStorage.setItem('token', result.data.data.token);
            this.$router.push('/home');
          } 
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    navigateToSignup() {
      this.$router.push("/signup");
    },
  },
};
</script>
<style lang="less" scoped>
.login_container {
  background-image: url("../assets/image/welcome.png");
  background-size: cover;
  background-position: center;
  height: 100vh;
  display: flex;
  justify-content: flex-end; /* 内容靠右 */
  align-items: center; /* 垂直居中 */
  padding-right: 50px; /* 可选：设置右边距，避免贴边 */
}

.headImage {
  height: 130px;
  width: 130px;
  margin: -75px auto 20px;
  border-radius: 50%;
  padding: 10px;
  background-color: #fff;
  box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
  }
}

.login_form {
  width: 500px;
  padding: 20px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 10px;
  box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.3);
  text-align: center;
  position: relative;
}

.btns {
  display: flex;
  justify-content: flex-end; /* 提交和重置按钮靠右 */
  gap: 10px; /* 按钮间距 */
  margin-top: 20px;
}

.info-section {
  margin-top: 30px;
  text-align: center; /* 注册提示居中 */
  
  p {
    font-size: 14px;
    color: #666;
  }

  el-button {
    font-size: 16px;
    color: #3498db;
    padding: 0;
    &:hover {
      text-decoration: underline;
    }
  }
}
</style>